package io.renren.controller;


import io.renren.common.constant.Constant;
import io.renren.common.page.PageData;
import io.renren.common.utils.Result;
import io.renren.modules.yckj.common.FileConfig;
import io.renren.modules.yckj.common.UploadUtil;
import io.renren.modules.yckj.dto.MajorsDTO;
import io.renren.modules.yckj.service.MajorsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 登录接口
 *
 * @author Mark lidong2010@gmail.com
 */
@RestController
@RequestMapping("/yckj/common")
@Tag(name = "通用接口")
public class ApiCommonController {

    @Autowired
    private FileConfig fileConfig;

    @Operation(summary = "上传文件接口")
    @PostMapping("/uploadFile")
    @ResponseBody
    public Result<Object> uploadFile(@RequestParam("file") MultipartFile file, String type) {
        String filePath = fileConfig.getFilePath() + "/" + type;
        String fileUrl = fileConfig.getFileUrl() + "/" + type;
        String fileName = UploadUtil.upload(file, filePath, false);
        String fileAllPath = fileUrl + "/" + fileName;
        Map<String, String> map = new HashMap<>();
        map.put("name", fileName);
        map.put("url", fileAllPath);
        return new Result<>().ok(map);
    }


    @Autowired
    private MajorsService majorsService;

    @GetMapping("school/page")
    @Operation(summary = "学校分页数据")
    @Parameters({
            @Parameter(name = Constant.PAGE, description = "当前页码，从1开始", in = ParameterIn.QUERY, required = true, ref = "int"),
            @Parameter(name = Constant.LIMIT, description = "每页显示记录数", in = ParameterIn.QUERY, required = true, ref = "int"),
            @Parameter(name = Constant.ORDER_FIELD, description = "排序字段", in = ParameterIn.QUERY, ref = "String"),
            @Parameter(name = Constant.ORDER, description = "排序方式，可选值(asc、desc)", in = ParameterIn.QUERY, ref = "String"),
            @Parameter(name = "key", description = "输入关键字，模糊查询", in = ParameterIn.QUERY, ref = "String")
    })
    @ApiResponse(content = @Content(mediaType = "application/json",
            schema = @Schema(implementation = MajorsDTO.class)))
    public Result<PageData<MajorsDTO>> majorsPage(@Parameter(hidden = true) @RequestParam Map<String, Object> params) {
        params.put("groupBy", true);
        PageData<MajorsDTO> page = majorsService.pageForApi(params);

        return new Result<PageData<MajorsDTO>>().ok(page);
    }

    @GetMapping("majors/list")
    @Operation(summary = "专业列表")
    @ApiResponse(content = @Content(mediaType = "application/json",
            schema = @Schema(implementation = MajorsDTO.class)))
    @Parameter(name = "schoolName", description = "学习名称", required = true)
    public Result<List<MajorsDTO>> majorsList(@RequestParam("schoolName") String schoolName) {
        Map<String, Object> params = new HashMap<>();
        params.put("schoolName", schoolName);
        List<MajorsDTO> list = majorsService.list(params);

        return new Result<List<MajorsDTO>>().ok(list);
    }


}


